package com.zeus.gen.base.dto;

/**
 * 分页查询对象
 *
 * @author liuwei
 */
public class Query4Page<T> extends Query<T> {

    /**
     * 当前页
     */
    private int curPage = 1;
    /**
     * 总页数
     */
    private int maxPage;
    /**
     * 总行数
     */
    private int maxRowCount;
    /**
     * 页大小
     */
    private int pageSize = 10;
    /**
     * 查询开始行
     */
    private Integer startRow = 0;
    /**
     * 查询结束行
     */
    private Integer endRow = pageSize;
    /**
     * 上一页
     */
    private int prePage;
    /**
     * 下一页
     */
    private int nextPage;

    public Query4Page() {
        super(true);
    }

    public int getCurPage() {
        return curPage;
    }

    public void setCurPage(int curPage) {
        if (curPage - 1 < 0) {
            curPage = 1;
        }
        this.curPage = curPage;
    }

    public int getMaxPage() {
        return maxPage;
    }

    public void setMaxPage(int maxPage) {
        this.maxPage = maxPage;
    }

    public int getMaxRowCount() {
        return maxRowCount;
    }

    public void setMaxRowCount(int maxRowCount) {
        this.maxRowCount = maxRowCount;
        this.setMaxPage();
    }

    /**
     * 根据记录总数和每页显示的记录条数，计算总共有多少页
     */
    public void setMaxPage() {
        if (this.maxRowCount % this.pageSize == 0) {
            this.maxPage = this.maxRowCount / this.pageSize;
        } else {
            this.maxPage = this.maxRowCount / this.pageSize + 1;
        }
        if (this.curPage > this.maxPage) {
            this.curPage = this.maxPage;
        }
    }

    public Integer getStartRow() {
        if ((curPage - 1) < 0) {
            curPage = 1;
        }
        return (curPage - 1) * pageSize;
    }

    public Integer getEndRow() {
        return curPage * pageSize;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPrePage() {
        this.prePage = curPage - 1 <= 0 ? 1 : curPage - 1;
        return prePage;
    }

    public int getNextPage() {
        this.nextPage = curPage + 1 >= maxPage ? maxPage : curPage + 1;
        return nextPage;
    }

}

